//
//  QRCodeCreateViewController.swift
//  swift_demo_weibo
//
//  Created by 杨兵 on 15/5/16.
//  Copyright (c) 2015年 bing. All rights reserved.
//  生成二维码

import UIKit
import SDWebImage

class QRCodeCreateViewController: UIViewController {

    @IBOutlet weak var iconview: UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()

        setupIconView()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    
    // 加载头像
    // http://weibo.com/2949737721/profile?topnav=1&wvr=6
    func setupIconView() {
        let stringvalue = "http://weibo.com/u/2949737721/"
        let url = NSURL(string: userInfoShared!.avatar_large!)
        
        // 获取头像
        SDWebImageManager.sharedManager().downloadImageWithURL(url, options: .allZeros, progress: nil) { (image, error, _, _, _) -> Void in
            NSOperationQueue .mainQueue().addOperationWithBlock({ () -> Void in
                self.iconview.image = self.generateImage(stringvalue, avatarImage: image)
            })
        }
        
    }
    
    
    // MARK: - 生成二维码
    // 输入字符串,要嵌入的图像,图像缩放比例,前景色,背景色
    func generateImage(stringValue: String, avatarImage: UIImage?,avatarScale:CGFloat = 0.25, color:CIColor = CIColor(red: 0, green: 0, blue: 0), backcolor:CIColor = CIColor(red: 1, green: 1, blue: 1)) -> UIImage {
        let qrFilter = CIFilter(name: "CIQRCodeGenerator")
        qrFilter.setDefaults()
        
        // 转换为二进制数据
        qrFilter.setValue(stringValue.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false), forKey: "inputMessage")
        let ciImage = qrFilter.outputImage
        let colorFilter = CIFilter(name: "CIFalseColor")
        colorFilter.setDefaults()
        colorFilter.setValue(color, forKey: "inputColor0")
        colorFilter.setValue(ciImage, forKey: "inputImage")
        colorFilter.setValue(backcolor, forKey: "inputColor1")
        
        let transform = CGAffineTransformMakeScale(5, 5)
        let transformImage = colorFilter.outputImage.imageByApplyingTransform(transform)
        
        let image = UIImage(CIImage: transformImage)
        if avatarImage != nil && image != nil {
            return insertAvaterImage(image!, avatarImage: avatarImage!, scale: avatarScale)
        }
        
        return image!
    }
    
    
    // 将图片嵌入二维码中
    func insertAvaterImage(codeImage: UIImage, avatarImage: UIImage, scale: CGFloat)-> UIImage {
        let rect = CGRectMake(0, 0, codeImage.size.width, codeImage.size.height)
        UIGraphicsBeginImageContext(rect.size)
        
        codeImage.drawInRect(rect)
        
        let avatarSize = CGSizeMake(rect.size.width * scale, rect.size.height * scale)
        let x = (rect.width - avatarSize.width) * 0.5
        let y = (rect.height - avatarSize.height) * 0.5
        avatarImage.drawInRect(CGRectMake(x, y, avatarSize.width, avatarSize.height))
        
        let result = UIGraphicsGetImageFromCurrentImageContext()
        
        UIGraphicsEndImageContext()
        
        return result
    }

    
    
}
